package com.PuzGame.combinenumbers;

import java.util.ArrayList;

import android.content.Context;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;


public class ComparatorAdapter extends ArrayAdapter<MyComparator> {
	protected Context context;
	private int LayoutId;
	OnClickListener listener;
	private ArrayAdapter<MyComparator> adapter;

	
	public ComparatorAdapter(Context c,
			int layoutId, int TextViewId,
			ArrayList<MyComparator> Comparators) {
		super (c, layoutId,TextViewId,Comparators);
		LayoutId = layoutId;
		context = c;
		adapter = this;
	}

	public void setItems(ArrayList<MyComparator> items) {
		this.addAll(items);
		this.notifyDataSetChanged();
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		

		View List;
		if(convertView==null){
			List=new View(context);
			LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			List=mLayoutinflater.inflate(LayoutId, parent, false);
		}
		else{
			List = (View)convertView;
		}
		
		
		final MyComparator item = ((MyComparator) getItem(position));
		
		TextView Title = (TextView)List.findViewById(R.id.ComparatorTitle);
		Title.setText(item.GetTiltle());
		
		ImageButton Order = (ImageButton) List.findViewById(R.id.ComparatorOrderIcon);
		
			Order.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					item.SwapOrder();

					if(listener != null)
						listener.onClick(v);
					adapter.notifyDataSetChanged();
				}
				
			});
		
		if(item.getOrder())
		{
			Order.setBackgroundResource(R.drawable.dec);
		}
		else
		{
			Order.setBackgroundResource(R.drawable.inc);
		}
		
		return List;
	}
	
	 @Override
	    public View getDropDownView(int position, View convertView,
	            ViewGroup parent) {

		 View List;
			if(convertView==null){
				List=new View(context);
				LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				List=mLayoutinflater.inflate(LayoutId, parent, false);
			}
			else{
				List = (View)convertView;
			}
			
			
			MyComparator item = ((MyComparator) getItem(position));
			
			TextView Title = (TextView)List.findViewById(R.id.ComparatorTitle);
			Title.setText(item.GetTiltle());
			
			ImageButton Order = (ImageButton) List.findViewById(R.id.ComparatorOrderIcon);
			
			
			Order.setVisibility(View.INVISIBLE);
			
			
			return List;
	    }

	public void setOnOrderChanged(OnClickListener listener) {
		
		this.listener = listener;
	}

}

